query("SHOW TABLES"); while($row = $result->fetch(PDO::FETCH_NUM)) { $tables[] = $row[0]; } $output = ""; // Add SQL header $output .= "-- Database Backup\n"; $output .= "-- Generated: " . date('Y-m-d H:i:s') . "\n"; $output .= "-- Database: {$DB_name}\n\n"; $output .= "SET FOREIGN_KEY_CHECKS=0;\n\n"; foreach($tables as $table) { // Drop table if exists $output .= "DROP TABLE IF EXISTS `$table`;\n"; // Create table structure $createTable = $GLOBALS['DBcon']->query("SHOW CREATE TABLE `$table`"); $row = $createTable->fetch(PDO::FETCH_NUM); $output .= $row[1] . ";\n\n"; // Insert data $output .= "-- Dumping data for table `$table`\n"; $data = $GLOBALS['DBcon']->query("SELECT * FROM `$table`"); $data->setFetchMode(PDO::FETCH_ASSOC); $insertQuery = "INSERT INTO `$table` ("; $columns = array(); $columnResult = $GLOBALS['DBcon']->query("SHOW COLUMNS FROM `$table`"); while($col = $columnResult->fetch(PDO::FETCH_ASSOC)) { $columns[] = "`" . $col['Field'] . "`"; } $insertQuery .= implode(", ", $columns) . ") VALUES\n"; $firstRow = true; while($row = $data->fetch()) { if($firstRow) { $output .= $insertQuery; $firstRow = false; } else { $output .= ",\n"; } $values = array(); foreach($row as $value) { $values[] = is_null($value) ? "NULL" : "'" . addslashes($value) . "'"; } $output .= "(" . implode(", ", $values) . ")"; } if(!$firstRow) { $output .= ";\n\n"; } else { $output .= "-- No data in table `$table`\n\n"; } } $output .= "SET FOREIGN_KEY_CHECKS=1;\n"; // Write to file if(file_put_contents($filepath, $output)) { return [ 'success' => true, 'filename' => $filename, 'filepath' => $filepath, 'size' => filesize($filepath) ]; } else { return [ 'success' => false, 'error' => 'Could not write backup file' ]; } } catch(Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } // Download backup file function downloadBackupFile($filename) { $backupDir = '../backups/'; $filepath = $backupDir . $filename; if(file_exists($filepath)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($filepath) . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($filepath)); readfile($filepath); exit; } } // Delete backup file function deleteBackupFile($filename) { $backupDir = '../backups/'; $filepath = $backupDir . $filename; if(file_exists($filepath)) { return unlink($filepath); } return false; } // Get existing backups function getBackupFiles() { $backupDir = '../backups/'; $backups = array(); if(file_exists($backupDir)) { $files = scandir($backupDir); foreach($files as $file) { if($file != '.' && $file != '..' && pathinfo($file, PATHINFO_EXTENSION) == 'sql') { $filepath = $backupDir . $file; $backups[] = [ 'filename' => $file, 'size' => filesize($filepath), 'modified' => filemtime($filepath), 'path' => $filepath ]; } } } // Sort by modification time (newest first) usort($backups, function($a, $b) { return $b['modified'] - $a['modified']; }); return $backups; } $backup_files = getBackupFiles(); ?> School Admin

Create Backup

Create a complete backup of your database. This will generate a SQL file containing all your data.

Database Information:
Name:
Host:
Tip: Regular backups protect your data from accidental loss or corruption.

System Information

Backup Directory:
Directory Status:
Total Backups:
Last Backup: 0 ? date('M j, Y H:i', $backup_files[0]['modified']) : 'Never'; ?>

Existing Backups

List of all available database backup files

No backup files found. Create your first backup to get started.
File Name Size Created Actions